<?php

namespace app\weixin\controller;

use think\Controller;
/**
 * 
 */
class Index extends Controller
{
	
	public function test()
	{
		$token = $this->get_token();

		var_dump($token);
	}

	public function get_token()
	{

		if(cache('token')){
			return cache('token');
		}else{

			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".config('site.APPID')."&secret=".config('site.APPSECRET');

			$data =  file_get_contents($url);

			$data = json_decode($data,true);

			cache('token',$data['access_token'],7200);

			return $data['access_token'];

		}
	}

	public function menu()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->get_token();


		$data = [
			'button'=>[
				[
					'type'=>'view',
					'name'=>'中国好声音',
					'url'=>'http://www.baidu.com'
				],
				[
					'type'=>'view',
					'name'=>'社区',
					'url'=>'http://1o533n8683.imwork.net'
				],
				[
					'name'=>'个人中心',
					'sub_button'=>[
						[
							'type'=>'view',
							'name'=>'头像修改',
							'url'=>'http://1o533n8683.imwork.net/index/user/set.html'
						],
						[
							'type'=>'view',
							'name'=>'密码',
							'url'=>'http://www.qq.com'
						],
						[
							'type'=>'view',
							'name'=>'中国声音2',
							'url'=>'http://www.163.com'
						]

					]

				],
			]
		];

		$this->post($url,$data);
		

	}

	public function post($url,$data)
	{
		$data = json_encode($data,JSON_UNESCAPED_UNICODE);

		$curl = curl_init();

		curl_setopt($curl, CURLOPT_URL, $url);

		curl_setopt($curl, CURLOPT_HEADER, false);

		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

		curl_setopt($curl, CURLOPT_POST,1);

		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

		curl_setopt($curl, CURLOPT_HTTPHEADER, array(
		    'Content-Type: application/json',
		    'Content-Length: ' . strlen($data)
		));

		$res = curl_exec($curl);

		var_dump($res);

		// $arr = json_decode($res,true);

		// var_dump($arr['results']);
	}

	public function getmenu()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$this->get_token();

		var_dump($this->get($url));
	}


	public function get($url)
	{
		$data =  file_get_contents($url);

		return $data = json_decode($data,true);
	}


	public function qunfa()
	{
		$userList = $this->getUsers();

		$openids = $userList['data']['openid'];

		$url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".$this->get_token();

		$data = [
			'touser'=>$openids,
			'msgtype'=>'text',
			'text'=>[
				'content'=>'大家好，欢迎关注'
			]
		];

		$this->post($url,$data);

		var_dump($openids);
	}

	public function getUsers()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->get_token();


		return $this->get($url);
	}

	public function moban()
	{
		$url= "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->get_token();

		$data = [
			'touser'=>'oF5Z2v5yzYfsCYP2oEDfz4ns94p0',
			'template_id'=>'xKMmLq3iJ5T1C7d1bKCr1XDYeOUJU1Xxo-MzRFFrzZc',
			"url"=>"http://www.baidu.com",
			'data'=>[
				"name"=>[
					'value'=>'小明',
					'color'=>'#173177'
				],
				"kname"=>[
					'value'=>'泡妞七十二技',
					'color'=>'#173177'
				],
				"time"=>[
					'value'=>date("Y-m-d H:i:s"),
					'color'=>'#173177'
				],
				"money"=>[
					'value'=>"200.00",
					'color'=>'#ED1C24'
				],
				"extime"=>[
					'value'=>"7天",
					'color'=>'#173177'
				],

			]
		];

		$this->post($url,$data);
	}


}
